package com.rongke.web.apix;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/2 0002.
 */
@CrossOrigin
public class BankCardAuth {
    private final static String APIX_KEY = "apix-key";

    private static String apixKey = "32fb51e07b1c4fb370de88f2b2c7c02a";

    public static JSONObject request(String type, String bankcardno, String name, String idcardno, String phone) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();

        //获取验证码接口地址
        String reqUrl = "http://v.apix.cn/apixcredit/idcheck/bankcard";

        HttpUriRequest request = RequestBuilder.get()//get请求
                .setUri(reqUrl)
                .addHeader(APIX_KEY, apixKey)
                .addParameter("type", type)
                .addParameter("bankcardno", bankcardno)
                .addParameter("name", name)
                .addParameter("idcardno", idcardno)
                .addParameter("phone", phone)
                .build();

        CloseableHttpResponse response = client.execute(request);

        //将字符串转换成jsonObject对象
        JSONObject myJsonObject = new JSONObject();
        myJsonObject = myJsonObject.parseObject(EntityUtils.toString(response.getEntity()));
        return myJsonObject;
    }

/*    public static void main(String[] args) {
        String httpUrl = " http://v.apix.cn/apixcredit/idcheck/bankcard";
        String httpArg = "type=银行卡核验类型&bankcardno=银行卡号&name=姓名&idcardno=身份证号&phone=预留手机号";
        String jsonResult = request(httpUrl, httpArg);
        System.out.println(jsonResult);
    }*/

    public static JSONObject limuBankCardAuth(String bankcardno, String name, String idcardno, String phone){
        JSONObject myJsonObject = new JSONObject();
        try{
            CloseableHttpClient client = HttpClients.createDefault();

            //获取验证码接口地址
            String reqUrl = "https://api.limuzhengxin.com/api/gateway";

            String sigo = "";
            String sigoStr = "apiKey=1860682371856456&bankCardNo="+bankcardno+"&identityNo="
                    +idcardno+"&method=api.identity.bankcard4check&mobileNo="+phone+"&name="+name+"&version=1.2.0"+"81TP9yRl8U4ET25KBCawSQTdJQKXmuYn";
            System.out.print(sigoStr);
            sigo = DigestUtils.shaHex(sigoStr);

            HttpUriRequest request = RequestBuilder.post()//get请求
                    .setUri(reqUrl)
                    .addParameter("method","api.identity.bankcard4check")
                    .addParameter("apiKey","1860682371856456")
                    .addParameter("version","1.2.0")
                    .addParameter("sign",sigo)
                    .addParameter("bankCardNo", bankcardno)
                    .addParameter("name", name)
                    .addParameter("identityNo", idcardno)
                    .addParameter("mobileNo", phone)
                    .build();

            CloseableHttpResponse response = client.execute(request);

            //将字符串转换成jsonObject对象
            myJsonObject = myJsonObject.parseObject(EntityUtils.toString(response.getEntity()));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return myJsonObject;
        }
    }
}
